package br.com.cliquetroca.embedded;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Hasher {
	
	private static final int HEX_255 = 0xFF;
	private static final String MD5_NAME = "MD5";

	public Hasher() {}

	public String hash(String senha) {
		MessageDigest algorithm = null;
		try {
			algorithm = MessageDigest.getInstance(MD5_NAME);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

		algorithm.reset();
		byte[] messageDigest = algorithm.digest(senha.getBytes());

		final StringBuilder hexString = new StringBuilder();
		for (int i = 0; i < messageDigest.length; i++) {
			final String hex = Integer.toHexString(HEX_255 & messageDigest[i]);
			if (hex.length() == 1) {
				hexString.append('0');
			}
			hexString.append(hex);
		}
		return hexString.toString();
	}
}